package com.pda.xinma.presenter;

import android.renderscript.ScriptC;

import com.pda.xinma.bean.CompleteBean;
import com.pda.xinma.bean.ConnectionBean;
import com.pda.xinma.bean.InStoregeListBean;
import com.pda.xinma.bean.OrderBean;
import com.pda.xinma.bean.ProcessBean;
import com.pda.xinma.bean.ScanResultBean;
import com.pda.xinma.bean.SelectBean;
import com.pda.xinma.bean.UserBean;
import com.pda.xinma.bean.WorkSurfaceBean;
import com.pda.xinma.bean.request.CompleteRequest;
import com.pda.xinma.bean.request.ConfirmEntry;
import com.pda.xinma.bean.request.QRcodeRequest;
import com.pda.xinma.bean.request.RecommendRequest;
import com.pda.xinma.contract.InStoregeContract;
import com.pda.xinma.net.NetUtils;
import com.wangshen.base.net.bean.BaseAppEntity;
import com.wangshen.base.ui.mvp.base.presenter.BasePresenter;

import java.util.List;

import io.reactivex.functions.Consumer;

public class InStoregePresenter extends BasePresenter<InStoregeContract.View>implements InStoregeContract.Presenter {

    @Override
    public void getCenterByUserId() {
        NetUtils.geApi().getCenterByUserId()
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<List<WorkSurfaceBean>>>() {
                    @Override
                    public void accept(BaseAppEntity<List<WorkSurfaceBean>> listBaseAppEntity) throws Exception {
                        getView().setFaceList(listBaseAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void getConnectionByCenter(String centerCode) {
        NetUtils.geApi().getConnectionByOrders(centerCode)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<List<ConnectionBean>>>() {
                    @Override
                    public void accept(BaseAppEntity<List<ConnectionBean>> listBaseAppEntity) throws Exception {
                        getView().setConnectionList(listBaseAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void getCenterList(String orderNo, String centerCode) {
        NetUtils.geApi().getCenterList(orderNo, centerCode)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<List<ProcessBean>>>() {
                    @Override
                    public void accept(BaseAppEntity<List<ProcessBean>> listBaseAppEntity) throws Exception {
                        getView().setProcessList(listBaseAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }
    @Override
    public void getCenterList2(String orderNo, String centerCode) {
        NetUtils.geApi().getCenterList2(orderNo, centerCode)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<List<ProcessBean>>>() {
                    @Override
                    public void accept(BaseAppEntity<List<ProcessBean>> listBaseAppEntity) throws Exception {
                        getView().setProcessList(listBaseAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }
    @Override
    public void getComplete(String orderNo, String processId) {
        NetUtils.geApi().getComplete(orderNo, processId)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<CompleteBean>>() {
                    @Override
                    public void accept(BaseAppEntity<CompleteBean> completeBeanBaseAppEntity) throws Exception {
                        getView().setComplete(completeBeanBaseAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void getUserByCenterCode(String orderNo) {
        NetUtils.geApi().getUserByCenterCode(orderNo)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<List<UserBean>>>() {
                    @Override
                    public void accept(BaseAppEntity<List<UserBean>> listBaseAppEntity) throws Exception {
                        getView().setUserList(listBaseAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }
    @Override
    public void getOrderByNo(String orderNo) {
        NetUtils.geApi().getOrderByNo(orderNo)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<OrderBean>>() {
                    @Override
                    public void accept(BaseAppEntity<OrderBean> orderBeanBaseAppEntity) throws Exception {
                        getView().setData(orderBeanBaseAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void scanTheQRCodeToVerify(String code) {
        QRcodeRequest request = new QRcodeRequest();
        request.setQRCode(code);
        NetUtils.geApi().scanTheQRCodeToVerify(request)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<ScanResultBean>>() {
                    @Override
                    public void accept(BaseAppEntity<ScanResultBean> scanResultBeanBaseAppEntity) throws Exception {
                        getView().setScanBean(scanResultBeanBaseAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void confirmEntry(ConfirmEntry request) {
        NetUtils.geApi().confirmEntry(request)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<UserBean>>() {
                    @Override
                    public void accept(BaseAppEntity<UserBean> userBeanBaseAppEntity) throws Exception {
                        getView().showMessage(userBeanBaseAppEntity.getMessage());
                        getView().onSuccess();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void recommendBin(RecommendRequest request) {
        NetUtils.geApi().recommendBin(request)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<String>>() {
                    @Override
                    public void accept(BaseAppEntity<String> userBeanBaseAppEntity) throws Exception {
                        List<RecommendRequest.OrderInfosBean> list=request.getOrderInfos();
                        for (RecommendRequest.OrderInfosBean bean: list){
                            bean.setStorageBin(userBeanBaseAppEntity.getData());
                        }
                        request.setOrderInfos(list);
                        getView().setRecommend(request);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void confirmComplete(CompleteRequest request) {
        NetUtils.geApi().confirmComplete(request)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<UserBean>>() {
                    @Override
                    public void accept(BaseAppEntity<UserBean> userBeanBaseAppEntity) throws Exception {
                        getView().showMessage(userBeanBaseAppEntity.getMsg());
                        getView().setSaveSuccess();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void getManzSpecList(InStoregeListBean inStoregeListBean) {
        NetUtils.geApi().getManzSpecList()
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<List<SelectBean>>>() {
                    @Override
                    public void accept(BaseAppEntity<List<SelectBean>> listBaseAppEntity) throws Exception {
                        getView().setManzList(listBaseAppEntity.getData(),inStoregeListBean);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }
}
